<?php

namespace App\Http\Controllers\Api\V1;

use App\Enums\ResponseCode;
use App\Exceptions\ApiExceptions;
use App\Http\Controllers\Api\AbstractApiController;
use App\Http\Resources\JobpositionDetailResource;
use App\Http\Resources\JobpositionResource;
use App\Http\Resources\JobpositionSingResource;
use App\Services\Jobposition\JobpositionService;
use App\Services\Jobposition\MessageService;
use App\Services\Jobposition\StatisticsService;
use App\Services\Search\SearchService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class JobpositionController extends AbstractApiController
{
    /**
     * 职位列表
     * @param Request $request
     * @param JobpositionService $service
     * @return JsonResponse
     */
    public function index(Request $request, JobpositionService $service): JsonResponse
    {
        $category_id = $request->input('category_id', null);
        $keyword = $request->input('keyword', null);
        $region_city = $request->input('region_city', '');
        //记录搜索记录
        $user = auth('api')->user();
        $result = $service->list(
            user: $user,
            categoryId: $category_id,
            keyword: $keyword,
            region_city: $region_city,
        );
       
        if ($user) {
            SearchService::store($user, $keyword);
        }
        return $this->success(trans("success"), ResponseCode::OK, new JobpositionResource($result));
    }

    /**
     * 职位详情
     * @param Request $request
     * @param JobpositionService $service
     * @param StatisticsService $statisticsService
     * @return JsonResponse
     * @throws ApiExceptions
     */
    public function detail(Request $request, JobpositionService $service,StatisticsService $statisticsService)
    {
        $input = $request->validate([
            'id' => 'required|int|min:1',
        ]);
        $user = auth('api')->user();
        $result = $service->detail(id: $input['id'], user: $user);
        $statisticsService->browse(job_position_id: $input['id'],user: $user,idfa: $request->header('idfa',''));
        return $this->success(trans("success"), ResponseCode::OK, new JobpositionDetailResource($result));
    }

    /**
     * 报名
     * @param Request $request
     * @param JobpositionService $service
     * @return JsonResponse
     * @throws ApiExceptions
     */
    public function signUp(Request $request, JobpositionService $service): JsonResponse
    {
        $input = $request->validate([
            'id' => 'required|int|min:1',
        ]);
        $user = auth('api')->user();
        $result = $service->signUp(id: $input['id'], user: $user);
        return $this->success('报名成功', ResponseCode::OK, new JobpositionSingResource($result));
    }

    /**
     * 收藏
     * @param Request $request
     * @param JobpositionService $service
     * @return JsonResponse
     * @throws ApiExceptions
     */
    public function collect(Request $request, JobpositionService $service): JsonResponse
    {
        $input = $request->validate([
            'id' => 'required|int|min:1',
        ]);
        $user = auth('api')->user();
        $service->collect(id: $input['id'], user: $user);
        return $this->success('收藏成功');
    }
    
     /**
     * 点赞
     * @param Request $request
     * @param JobpositionService $service
     * @return JsonResponse
     * @throws ApiExceptions
     */
    public function jobthumbsup(Request $request, JobpositionService $service): JsonResponse
    {
        $input = $request->validate([
            'id' => 'required|int|min:1',
        ]);
        $user = auth('api')->user();
        $service->jobthumbsup(id: $input['id'], user: $user);
        return $this->success('点赞成功');
    }

    /**
     * 取消点赞
     * @param Request $request
     * @param JobpositionService $service
     * @return JsonResponse
     * @throws ApiExceptions
     */
    public function jobthumbsup_cancel(Request $request, JobpositionService $service): JsonResponse
    {
        $input = $request->validate([
            'id' => 'required|int|min:1',
        ]);
        $user = auth('api')->user();
        $service->jobthumbsupCancel(job_position_id: $input['id'], user: $user);
        return $this->success(trans("success"));
    }

    /**
     * 取消收藏
     * @param Request $request
     * @param JobpositionService $service
     * @return JsonResponse
     * @throws ApiExceptions
     */
    public function collectCancel(Request $request, JobpositionService $service): JsonResponse
    {
        $input = $request->validate([
            'id' => 'required|int|min:1',
        ]);
        $user = auth('api')->user();
        $service->collectCancel(job_position_id: $input['id'], user: $user);
        return $this->success(trans("success"));
    }

    /**
     * 收藏列表
     * @param JobpositionService $service
     * @return JsonResponse
     */
    public function collectList(JobpositionService $service): JsonResponse
    {
        $user = auth('api')->user();
        $result = $service->collectList(user: $user);
        return $this->success(trans("success"), ResponseCode::OK, new JobpositionResource($result));
    }
    /**
     * 推荐列表
     * @param JobpositionService $service
     * @return JsonResponse
     */
    public function recommendList(JobpositionService $service): JsonResponse
    {
        $user = auth('api')->user();
        $result = $service->recommendList(user: $user);
        return $this->success(trans("success"), ResponseCode::OK, new JobpositionResource($result));
    }

    /**
     * 增加聊天类容
     * @param Request $request
     * @param MessageService $service
     * @return JsonResponse
     * @throws ApiExceptions
     */
    public function message(Request $request, MessageService $service): JsonResponse
    {
        $input = $request->validate([
            'job_position_id' => 'required|int',
            'content' => 'required',
        ]);
        $service->storage(
            userId: auth('api')->id(),
            job_position_id: $input['job_position_id'],
            content: $input['content']
        );
        return $this->success(trans("success"));
    }

    /**
     * 获取客服聊天内容
     * @param MessageService $service
     * @return JsonResponse
     */
    public function customerServiceMessage( MessageService $service): JsonResponse
    {
        $result['info'] = [
            'type' =>1,
            'content'=>'2111796139',
        ];
        $result['service'] = $service->customerServiceMessage(
            userId: auth('api')->id());
        return $this->success(trans("success"),ResponseCode::OK, $result);
    }
}